$(function () {
    initDatagrid();
    $("#queryButton").click(function () {
        $("#listTable").datagrid('load', {
            groupType: $.util.getRequest('dictKey'),
            queryStr: $("#queryStr").val(),
            beginTime: $("input[name=beginTime]").val(),
            endTime: $("input[name=endTime]").val()
        })
    });
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/sysMessage.listByGroup.json');
    $('#listTable').datagrid({
        url: url ,
        idField: 'id',
        fit: true,
        pagination: true,
        pageSize: 30,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [[
            {
                field: 'informTitle',
                title: '标题',
                align: 'center',
                width: 15
            },
            {
                field: 'createPerson',
                title: '发布人',
                align: 'center',
                width: 15,
                formatter: function (value, row) {
                    return value == null ? "——" : value;
                }
            },
            {
                field: 'createTime',
                title: '发起时间',
                align: 'center',
                width: 15
            },
            {
                field: 'haveRead',
                title: '已阅读',
                align: 'center',
                width: 15
            },
            {
                field: 'beginTime',
                title: '生效时间',
                align: 'center',
                width: 15
            },
            {
                field: 'endTime',
                title: '失效时间',
                align: 'center',
                width: 15
            },
            {
                field: 'context',
                width: 20,
                title: '操作',
                align: 'center',
                formatter: function (value, row) {
                    var aEl = '<a href=javascript:findDetail("{0}"); class="easyui-tooltip" title="查看">查看</a>' +
                        ' | <a href=javascript:editNotice("{0}"); class="easyui-tooltip" title="删除">编辑</a>' +
                        ' | <a href=javascript:deleteNotice("{0}"); class="easyui-tooltip" title="删除">删除</a>';
                    //格式化操作按钮
                    return $.string.format(aEl, row.id);
                }
            }
        ]]
    });
}

// 添加新公告或积分规则
function editNotice(id) {
    var title = id ? "编辑" : "新增";
    var url = $.util.getRootPath() + '/rsframe/page/notice/publishNotice.html';
    url = id ? url + '?id=' + id : url;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 800,
        height: 600,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '保存',
        onSave: function () {
            subForm(dialog);
        }
    });
}

// 查看详情
function findDetail(id) {
    var title = "查看";
    var url = $.util.getRootPath() + '/rsframe/page/notice/noticeDetail.html?id=' + id;
    var dialog = $.easyui.showDialog({
        iconCls: "",
        title: title,
        width: 600,
        height: 600,
        maximizable: true,
        href: url,
        topMost: true,
        saveButtonText: '发布',
        enableSaveButton: false
    });

}

// 删除公告内容
function deleteNotice(id) {
    $.messager.confirm('确认对话框', '您确定要删除该条公告信息吗？', function (r) {
        if (r) {
            var deleteUrl = "/json/operator.notice.deleteById.json";
            var url = $.util.getRequestJson('/json/sysMessage.updateStatus.json');
            $.util.get(url, {id: id, status: -1}, function (res) {
                // 删除完成重新加载
                if (res.success) {
                    $('#listTable').datagrid('reload');
                }
            });
        }
    });
}

// 保存项目内容
function subForm(dialog) {
    var messageType = parent.$("#messageType").combobox('getValue');
    if (parent.$("#context").ueditor('getPlainTxt') == "") {
        parent.$.messager.alert('请先填写发布信息的内容');
        return;
    }
    var url = $.util.getRootPath() + '/sysMessage/save';
    var form = parent.$('#publishNotice').form('submit', {
        url: url,
        onSubmit: function (param) {
            return parent.$('#publishNotice').form('validate');
        },
        success: function (data) {
            if (data.success) {
                $('#listTable').datagrid('reload');
                dialog.dialog('close');
            }
            $.easyui.messager.show(data.message);
        }
    });
}

